package com.yuzhihao.writejvm.attributes.runtimeva;

import com.yuzhihao.writejvm.attributes.Attributes;
import com.yuzhihao.writejvm.clazz.ClassInfo;
import com.yuzhihao.writejvm.read.Parse;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.log4j.Log4j2;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * RuntimeVisibleParameterAnnotationsAttribute  和 RuntimeInvisibleParameterAnnotationsAttribute 结构是异一样的
 * <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.18">RuntimeVisibleParameterAnnotationsAttribute</a>
 *
 * @author yzh
 * @since 2021年12月11日15:20:10
 */
@Data
@Log4j2
@EqualsAndHashCode(callSuper = true)
public class RuntimeVisibleParameterAnnotationsAttribute extends Attributes {

    protected int num_parameters_u1 = 1;

    protected int num_parameters;

    protected List<ParameterAnnotations> pas = new ArrayList<>();

    public RuntimeVisibleParameterAnnotationsAttribute(String name){
        super();
        super.setAttribute_name(name);
    }

    @Override
    public void parse(DataInputStream dis, ClassInfo ci) {
        try {
            super.attribute_length = Parse.byteToInt(super.attribute_length_u4, dis);
            this.num_parameters = Parse.byteToInt(this.num_parameters_u1, dis);

            int i = 0;
            while (i < this.num_parameters){
                ParameterAnnotations pa = new ParameterAnnotations();
                pa.parse(dis,ci);
                this.pas.add(pa);
                i++;
            }
        } catch (IOException e) {
            log.error(e);
        }
    }

}
